package com.example.juheweixin;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<WeixinItem> contents;
	private DisplayImageOptions options;
	public MyAdapter(Context context, ArrayList<WeixinItem> contents) {
		this.context = context;
		this.contents = contents;
		options = ImageLoaderHelper.getOptions(R.drawable.ic_launcher);
	}
	
	public void changeUi(ArrayList<WeixinItem> contents) {
		this.contents = contents;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return contents == null ? 0 : contents.size();
	}

	@Override
	public WeixinItem getItem(int position) {
		// TODO Auto-generated method stub
		return contents == null ? null : contents.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.view_list_item, null);
			holder.image = (ImageView) convertView.findViewById(R.id.image);
			holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			holder.tvSource = (TextView) convertView.findViewById(R.id.tv_source);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		WeixinItem item = getItem(position);
		if(item != null){
			ImageLoader.getInstance().displayImage(item.getFirstImg(), holder.image, options);
			holder.tvTitle.setText(item.getTitle());
			holder.tvSource.setText(item.getSource());
		}
	
		return convertView;
	}
	
	class ViewHolder{
		ImageView image;
		TextView tvTitle;
		TextView tvSource;
	}

}
